﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SFJDManagement
{
    /// <summary>
    /// 单项选择按钮组
    /// UC_MultiComboBox.xaml 的交互逻辑
    /// </summary>
    public partial class UC_MultiComboBox : UserControl
    {
        private UC_StatisticsPanel statisticsPanel;
        public StackPanel layout = new StackPanel();
        public delegate void CheckedChangeHandler(object sender, EventArgs e);
        public event CheckedChangeHandler CheckedChange;

        public UC_MultiComboBox(UC_StatisticsPanel panel)
        {
            statisticsPanel = panel;
            InitializeComponent();
            Ini();
        }

        public void Reflesh(UC_StatisticsPanel panel)
        {
            if (panel == statisticsPanel)
            {
                return;
            }
            else
            {
                statisticsPanel = panel;
                Ini();
            }
        }

        public void ChangeVisible(string name)
        {
            foreach(StackPanel panel in layout.Children)
            {
                if(panel.Name == name)
                {
                    panel.Visibility = panel.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
                    break;
                }
            }
        }

        private void Ini()
        {
            layout.Children.Clear();
            layout.Orientation = Orientation.Horizontal;
            foreach(StackPanel panel in ((statisticsPanel.Content as ScrollViewer).Content as StackPanel).Children)
            {
                string name = panel.Name;
                Dictionary<string, int> dictionary = (panel.Children[1] as DataGrid).ItemsSource as Dictionary<string, int>;
                List<string> list = dictionary.Keys.ToList();
                layout.Children.Add(GetPanel(name, list));
            }
            Content = layout;
            VerticalAlignment = VerticalAlignment.Stretch;
            Background = Brushes.AliceBlue;
        }

        private StackPanel GetPanel(string name, List<string> list)
        {
            StackPanel b = new StackPanel();
            b.Orientation = Orientation.Vertical;
            b.Name = name;
            Label label = new Label() { Content = name};
            b.Children.Add(label);
            list.Insert(0, "---全 部---");
            ComboBox comboBox = new ComboBox() { ItemsSource = list, Name = name};
            comboBox.SelectedIndex = 0;
            comboBox.SelectionChanged += ComboBox_SelectionChanged;
            b.Children.Add(comboBox);
            b.Margin = new Thickness(10, 0, 10, 0);
            return b;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CheckedChange(sender, e);
        }
    }
}
